//
//  CurveListViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/6/30.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "CurveListViewController.h"
#import "CurveListTableViewCell.h"
#import "CurveViewController.h"
#define kPageSize @"20"
#define kCellName @"CurveListTableViewCell"

@interface CurveListViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _pageIndex;
}
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation CurveListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self UISetting];
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)UISetting{
    _dataArray = [NSMutableArray array];
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageIndex = 0;
        [self loadData];
        
        [_tableView.mj_header endRefreshing];
    }];
    
    _tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _pageIndex ++;
        [self loadData];
        [_tableView.mj_footer endRefreshing];
    }];
    
}

#pragma mark - 数据下载

- (void)loadData{
    [self.hub hideAnimated:YES];
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    // Set the label text.
    self.hub.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
//    NSLog(@"访问");

    NSString *pageIndexString = [NSString stringWithFormat:@"%ld",_pageIndex];
    [[AFClient shareInstance] getShopSimpleListByEid:kUserId page_size:kPageSize page_index:pageIndexString status:@"1" progressBlock:nil success:^(id responseBody) {
        [self.hub hideAnimated:YES];
        if ([responseBody[@"code"] integerValue] == 200) {
            if (_pageIndex == 0) {
                [_dataArray removeAllObjects];
            }
            for (NSDictionary *dic in responseBody[@"data"]) {
                ShopModel *model = [[ShopModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_dataArray addObject:model];
            }
            [self creatImageBackView:_dataArray];
            [_tableView reloadData];
        }else{
            kAlert(responseBody[@"message"]);
        }
        
    } failure:^(NSError *error) {
        [self.hub hideAnimated:YES];
    }];
}

#pragma mark - 点击事件
- (IBAction)backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - tableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CurveListTableViewCell *cell = (CurveListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kCellName];
    ShopModel *model = _dataArray[indexPath.row];
    [cell showDataWithModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIStoryboard *storyboard = kMainStroyboard;
    CurveViewController *curveVC = [storyboard instantiateViewControllerWithIdentifier:@"CurveViewController"];
    ShopModel *model = _dataArray[indexPath.row];
    curveVC.shopId = model.id;
    curveVC.shopName = model.name;
    curveVC.typString = @"0";
    curveVC.beginDate = [BGDateHelper getTimeArrayByTimeString:[BGDateHelper getTimeStempByString:model.created havehh:YES]][0];
    [self.navigationController pushViewController:curveVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
